package com.clouds.wms.dao;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import com.clouds.wms.bean.Salmd;
import com.clouds.wms.exception.DatabaseException;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;

@Repository("salmdDao")
public class SalmdDaoImpl implements SalmdDao {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="namedParameterJdbcTemplate")
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	@Override
	public PagesBean getSalmdList(String mcaa, String mcab, int offset,int pageSize) throws DatabaseException {
		PagesBean pb = new PagesBean();
		List<Salmd> list = new ArrayList<Salmd>();
		StringBuffer sql = new StringBuffer("SELECT * FROM salmd ");
		sql.append( " where mdaa = '" + Function.formatSQLString(mcaa) + "'");
		sql.append( " and mdab = '" + Function.formatSQLString(mcab) + "'");
		sql.append(" ORDER BY mdad DESC");
		try {
			int totalCount = namedParameterJdbcTemplate.getJdbcOperations().queryForInt(Function.toCountSql(sql.toString()));
			if(totalCount!=0){
				list = namedParameterJdbcTemplate.getJdbcOperations().query(Function.mySqlQuery(sql.toString(),offset,pageSize,totalCount),new BeanPropertyRowMapper<Salmd>(Salmd.class));
			}
			pb.setCount(totalCount);
			pb.setData(list);
			return pb;
		} catch (EmptyResultDataAccessException e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int getMaxMdac()throws DatabaseException {
		int mdac = namedParameterJdbcTemplate.getJdbcOperations().queryForInt("select IFNULL(max(mdac),0) mdac from salmd ");
		if(mdac==0){
			mdac = 10000;
		}else{
			mdac += 10000;
		}
		return mdac;
	}
	@Override
	public int saveSalmd(Salmd salmd) throws DatabaseException {
		String sql = "INSERT INTO salmd (mdaa,mdab,mdac,mdad,mdae,mdaf,mdag,mdah,mdai,mdaj,mdak,mdal,mdam,mdan,mdao,mdap) VALUES (:mdaa,:mdab,:mdac,:mdad,:mdae,:mdaf,:mdag,:mdah,:mdai,:mdaj,:mdak,:mdal,:mdam,:mdan,:mdao,:mdap)";
		try {
			SqlParameterSource param = new BeanPropertySqlParameterSource(salmd);
			return namedParameterJdbcTemplate.update(sql, param);
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int delSalmd(String mdaa,String mdab,String mdacs) throws DatabaseException {
		String sql = "DELETE FROM salmd WHERE mdaa=? and mdab=? and mdac in ("+mdacs+")";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().update(sql, new Object[]{mdaa,mdab});
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int updateSalmd(Salmd salmd) throws DatabaseException {
		String sql = "UPDATE salmd SET mdad=:mdad, mdae=:mdae,mdaf=:mdaf,mdag=:mdag,mdah=:mdah,mdai=:mdai,mdaj=:mdaj, mdak=:mdak,mdal=:mdal,mdam=:mdam,mdan=:mdan WHERE mdaa=:mdaa and mdab = :mdab and mdac =:mdac";
		try {
			SqlParameterSource param = new BeanPropertySqlParameterSource(salmd);
			return namedParameterJdbcTemplate.update(sql, param);
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int changeStatus(String mdaa, String mdab, String status)throws DatabaseException {
		String sql = "UPDATE salmc SET mcac=? WHERE mcaa=? and mcab = ?";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().update(sql, new Object[]{status,mdaa,mdab});
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public void delSalmdByAaAb(String maaa, String maab) throws DatabaseException {

		String sql2 = "delete from salmd where mdaa = ? and mdab = ?";
		namedParameterJdbcTemplate.getJdbcOperations().update(sql2, new Object[]{maaa,maab});
		
		
	}
}
